package simulador;

import eduni.simjava.Sim_entity;
import eduni.simjava.Sim_event;
import eduni.simjava.Sim_port;
import eduni.simjava.Sim_stat;
import eduni.simjava.Sim_system;
import eduni.simjava.distributions.Sim_negexp_obj;
import eduni.simjava.distributions.Sim_normal_obj;

public class Disk extends Sim_entity {
	private Sim_port in, outLocker, out;
	private Sim_normal_obj delay;
	private Sim_stat stat;

	Disk(String name) {
		super(name);
		// Port for receiving events from the processor
		in = new Sim_port("In");
		outLocker = new Sim_port("OutLocker");
		out = new Sim_port("Out");
		add_port(in);
		add_port(outLocker);
		add_port(out);
		stat = new Sim_stat();
		stat.add_measure(Sim_stat.ARRIVAL_RATE);
		stat.add_measure(Sim_stat.QUEUE_LENGTH);
		// stat.add_measure(Sim_stat.RESIDENCE_TIME);
		stat.add_measure(Sim_stat.SERVICE_TIME);
		stat.add_measure(Sim_stat.THROUGHPUT);
		stat.add_measure(Sim_stat.UTILISATION);
		// stat.add_measure(Sim_stat.WAITING_TIME);
		set_stat(stat);
	}

	public Disk(String name, double delayTime, long seed) {
		this(name);
		delay = new Sim_normal_obj("delay", delayTime, delayTime * 0.05, seed);
		// delay = new Sim_negexp_obj("delay", seed);
	}

	public Disk(String name, double delayTime) {
		this(name);
		delay = new Sim_normal_obj("delay", delayTime, delayTime * 0.05);
		// delay = new Sim_negexp_obj("delay", seed);
	}

	public void body() {
		while (Sim_system.running()) {
			Sim_event e = new Sim_event();
			// Get the next event
			sim_get_next(e);
			if (e.get_tag() == -1)
				continue;
			// Get id source locker
			// int idLocker = e.get_src();
			// Get the event's data
			Job job = (Job) e.get_data();
			// Process the event
			sim_process(delay.sample());
			// sim_process(60000);
			// The event has completed service
			sim_completed(e);
			// Send job to CPU
			sim_trace(4, "Job(" + e.get_tag() + ") returning to CPU");
			// sim_schedule(out, 0, 1, job);
			sim_schedule(out, 0, e.get_tag(), job);
			// Send a event to unlock the locker.
			// sim_trace(4, "Disk unlocking locker");
			// sim_schedule(idLocker, 0, e.get_tag(), job);
		}
	}
}
